package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;



    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectPageSetmeal(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    public void startOrStopSetmeal(Integer status, Long id) {
        //起售时，判断套餐内是否有停售菜品，有停售菜品提示”套餐内包含未起售菜品，无法起售“
        if(status == StatusConstant.ENABLE){//前端传过来的状态为启用时 查询一下该套餐中是否有停售的商品
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() >0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateSetmeal(setmeal);

        //setmealMapper.updateSetmealById(status,id);
    }

    /**
     * 根据id查询套餐
     * 用于修改页面的数据回显
     * @param id
     * @return
     */
    public SetmealVO querySetmeal(Long id) {

        //根据setmeal_id查询name
        List<SetmealDish> setmealDish = setmealDishMapper.selectNameBySetmealId(id);


        //查询套餐
        SetmealVO setmealvo = setmealMapper.selectSetmealById(id);
        setmealvo.setSetmealDishes(setmealDish);

        return setmealvo;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {


        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //修改套餐下的菜品数据时还是先删除再插入
        setmealDishMapper.deleteSetmealDish(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.insertSetmealDish(setmealDishes);

    }


    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {

        //根据分类id分组查询查询下面的菜品 不用查询
        //在dish中增加一个根据分类id查询菜品的接口即可
       // List<Dish> dish = dishMapper.selectDishByCategoryId(setmealDTO.getCategoryId());

        //批量插入菜品  不是根据执行顺序来的
        //List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //setmealDishMapper.insertSetmealDish(setmealDishes);


        //插入套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetmeal(setmeal);

        //获取生成的套餐id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //保存套餐和菜品的关联关系
        setmealDishMapper.insertSetmealDish(setmealDishes);

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        //起售中的套餐不能删除
        ids.forEach(id -> {
            SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
            if(StatusConstant.ENABLE == setmealVO.getStatus()){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        ids.forEach(setmealId ->{
            //删除套餐表中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteSetmealDish(setmealId);
        });


        //setmealMapper.deleteBatch(ids);

    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    public List<Setmeal> querySetmealByCategoryId(Long categoryId) {
        List<Setmeal> setmealList = setmealMapper.selectSetmealByCategoryId(categoryId);
        return setmealList;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    public List<DishItemVO> querySetmealDishBySetmealId(Long id) {
        //从setmeal_dish中获取 name copies
        List<DishItemVO> dishItemVOList = setmealDishMapper.selectSetmealDishBySetmealId(id);
        //从setmeal中获取description和image
        SetmealVO setmealVO = setmealMapper.selectSetmealById(id);

        //将从setmeal中获取description和image放入dishItemVOList
        for (DishItemVO dishItem : dishItemVOList) {
            dishItem.setDescription(setmealVO.getDescription());
            dishItem.setImage(setmealVO.getImage());
        }
        return dishItemVOList;
    }
}
